package lile;

import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class DoubleColorBall {
    public static void main(String[] args) {
        int[] winningNumbers = generateWinningNumbers();
        int[] userNumbers = getUserInputNumbers();
        System.out.print("中奖号码为：");
        for (int num : winningNumbers) {
            System.out.print(num + " ");
        }
        System.out.println();
        System.out.print("您投注号码为：");
        for (int num : userNumbers) {
            System.out.print(num + " ");
        }
        System.out.println();
        int[] hitCount = calculateHitCount(winningNumbers, userNumbers);
        System.out.println("您命中了" + hitCount[0] + "个红色球号码，" + hitCount[1]
                + "个蓝色球号码。");
        String prizeLevel = determinePrizeLevel(hitCount[0], hitCount[1]);
        System.out.println(prizeLevel);
    }

    // 生成中奖号码
    public static int[] generateWinningNumbers() {
        Random random = new Random();
        Set<Integer> redBalls = new HashSet<>();
        while (redBalls.size() < 6) {
            redBalls.add(random.nextInt(33) + 1);
        }
        int[] numbers = new int[7];
        int i = 0;
        for (int ball : redBalls) {
            numbers[i++] = ball;
        }
        numbers[6] = random.nextInt(16) + 1;
        return numbers;
    }

    // 获取用户输入的号码
    public static int[] getUserInputNumbers() {
        Scanner scanner = new Scanner(System.in);
        Set<Integer> redBalls = new HashSet<>();
        int[] numbers = new int[7];
        // 输入红球号码
        for (int i = 0; i < 6; i++) {
            while (true) {
                System.out.println("请输入红球号码，输入任意一个范围1-33的数字：");
                int num = scanner.nextInt();
                if (num < 1 || num > 33) {
                    System.out.println("号码超出范围，请重新输入！");
                } else if (redBalls.contains(num)) {
                    System.out.println("号码重复，请重新输入！");
                } else {
                    redBalls.add(num);
                    numbers[i] = num;
                    break;
                }
            }
        }
        // 输入蓝球号码
        while (true) {
            System.out.println("请输入蓝球号码，输入任意一个范围1-16的数字：");
            int num = scanner.nextInt();
            if (num < 1 || num > 16) {
                System.out.println("号码超出范围，请重新输入！");
            } else {
                numbers[6] = num;
                break;
            }
        }
        return numbers;
    }

    // 计算命中的红球和蓝球数量
    public static int[] calculateHitCount(int[] winningNumbers, int[] userNumbers) {
        int redHitCount = 0;
        int blueHitCount = 0;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                if (winningNumbers[i] == userNumbers[j]) {
                    redHitCount++;
                }
            }
        }
        if (winningNumbers[6] == userNumbers[6]) {
            blueHitCount = 1;
        }
        return new int[]{redHitCount, blueHitCount};
    }

    // 确定中奖级别
    public static String determinePrizeLevel(int redHitCount, int blueHitCount) {
        if (redHitCount == 6 && blueHitCount == 1) {
            return "恭喜您中了一等奖！";
        } else if (redHitCount == 6 && blueHitCount == 0) {
            return "恭喜您中了二等奖！";
        } else if (redHitCount == 5 && blueHitCount == 1) {
            return "恭喜您中了三等奖！";
        } else if (redHitCount == 5 && blueHitCount == 0 || redHitCount == 4
                && blueHitCount == 1) {
            return "恭喜您中了四等奖！";
        } else if (redHitCount == 4 && blueHitCount == 0 || redHitCount == 3
                && blueHitCount == 1) {
            return "恭喜您中了五等奖！";
        } else if ((redHitCount == 2 && blueHitCount == 1) || (redHitCount == 1
                && blueHitCount == 1) || (redHitCount == 0 && blueHitCount == 1)) {
            return "恭喜您中了六等奖！";
        } else {
            return "很抱歉，您未中奖！";
        }
    }
}
